package com.imooc.service;

import com.imooc.bean.Command;
import com.imooc.bean.CommandContent;
import com.imooc.bean.Message;
import com.imooc.config.PageConfig;
import com.imooc.dao.CommandDao;
import com.imooc.dao.MessageDao;
import com.imooc.enums.Iconst;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 列表相关的业务功能
 */
public class QueryService {
    public List<Message> queryList(String command, String description) {
        MessageDao messageDao = new MessageDao();
        return messageDao.queryList(command, description);
    }

    public List<Message> queryListByPage(String command, String description, PageConfig pageConfig) {
        Map<String, Object> parameter = new HashMap<>();
        Message message = new Message();
        message.setCommand(command);
        message.setDescription(description);
        parameter.put("message", message);
        parameter.put("pageConfig", pageConfig);
        return new MessageDao().queryListByPage(parameter);
    }

    public String queryByCommand(String command) {
        CommandDao commandDao = new CommandDao();
        List<Command> commandList = null;
        if(Iconst.HELP_COMMAND.getDescription().equals(command.trim())) {
            commandList = commandDao.queryCommandList(null, null);
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < commandList.size(); i++) {
                if(i != 0) {
                    sb.append("</br>");
                }
                sb.append("回复[" + commandList.get(i).getName() + "]可以查看" + commandList.get(i).getDescription());
            }
            return sb.toString();
        }
        commandList = commandDao.queryCommandList(command, null);
        if(commandList.size() > 0) {
            List<CommandContent> contentList = commandList.get(0).getContentList();
            int index = new Random().nextInt(contentList.size());
            return contentList.get(index).getContent();
        }
        return Iconst.NO_MATCHING_CONTENT.getDescription();
    }
}
